<?php

declare(strict_types=1);

/** 调用oss组件对图片进行处理
 * User：Abell
 * Date：2020/5/8
 */

namespace App\Controller\Abell;

use App\Controller\AbstractController;
use App\Model\Wxuser as WxuserModel;
use App\Utils\ApiUtils;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;

/**
 * @Controller()
 */
class ImgController extends AbstractController
{

    /**
     * @RequestMapping(path="/abell/img/showlist", methods="post,get")
     */
    public function showlist(){

    }

    /** post 上传图片
     * @RequestMapping(path="/abell/img/img", methods="post")
     */
    public function img(RequestInterface $request,ResponseInterface $response){
        if($request->hasFile('img')){
            // post请求带资源文件上传
            $upload_img = make(Ossfile::class)->upload($request->file('img'));
            return $upload_img ? $response->json(ApiUtils::send(0,'ok',$upload_img,1)):$response->json(ApiUtils::send(400,'上传失败',$upload_img,1));
        }else{return $this->response->json(ApiUtils::send(400,'参数错误',false,1));}
    }

    /** get带参数显示图片
     * @RequestMapping(path="/abell/img/showimg", methods="get")
     */
    public function showimg(RequestInterface $request,ResponseInterface $response){
        if($request->has('img')){
            $imgurl = make(Ossfile::class)->show($request->input('img'));
            return $imgurl ? $response->json(ApiUtils::send(0,'ok',$imgurl,1)):$response->json(ApiUtils::send(400,'资源不存在',$imgurl,1));
        }else{return $this->response->json(ApiUtils::send(400,'参数错误',false,1));}
    }


    /** 循环遍历处理用户呢称改成base64编码
     * @RequestMapping(path="/abell/img/index", methods="post,get")
     */
    public function index(RequestInterface $request, ResponseInterface $response){

        $chagename = WxuserModel::query()->get()->toArray();
        foreach ($chagename as $cname) {
//            var_dump($cname['nickname']);
            $base64name = base64_encode($cname['nickname']);
            var_dump($base64name);
            WxuserModel::query()->where(['id'=>$cname['id']])->update([
                'nickname' => $base64name,
//                'hasadd'   => 1 ,
                'update_time' => time()
            ]);
        }
    }

}